{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/member.css" />
<style>
    .ns-form {margin-top: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form ns-form">

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>选择用户：</label>
        <div class="layui-input-inline ns-len-mid">
            <select class="member_level" name="member_id" lay-verify="required" lay-filter="member_id">
                <option value="">请选择</option>
                {volist name="member_list" id="member"}
                <option value="{$member.member_id}" >{$member.nickname}/{$member.mobile}</option>
                {/volist}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>会员类型：</label>
        <div class="layui-input-inline ns-len-mid">
            <select class="member_level" name="member_card_type" lay-verify="required" lay-filter="member_card_type">
                <option value="1">个人会员</option>
                <option value="2">门店会员</option>
                <option value="3">员工会员</option>
            </select>
        </div>
    </div>

    <div class="ns-form-row">
        <button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
        <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
    </div>
</div>
{/block}
{block name="script"}
<script>
    layui.use(['form', 'laydate'], function() {
        var form = layui.form,
            laydate = layui.laydate,
            repeat_flag = false; //防重复标识
        form.render();

        laydate.render({
            elem: '#laydate'
        });

        /**
         * 表单验证
         */
        form.verify({
            isemail: function(value) {
                var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
                if (value == '') {
                    return;
                }
                if (!reg.test(value)) {
                    return '请输入正确的邮箱!';
                }
            }
        });

        form.on('submit(save)', function(data) {

            data.field.member_level_name = $(".member_level").find("option[value=" + data.field.member_level + "]").text();

            if(repeat_flag) return false;
            repeat_flag = true;

            //图片删除
            if(!data.field.headimg) headImg_upload.delete();

            $.ajax({
                url: ns.url("admin/member/addMember"),
                data: data.field,
                dataType: 'JSON', //服务器返回json格式数据
                type: 'POST', //HTTP请求类型
                success: function(res) {
                    repeat_flag = false;
                    if (res.code == 0) {
                        layer.confirm('添加成功', {
                            title:'操作提示',
                            btn: ['返回列表', '继续添加'],
                            yes: function(){
                                location.href = ns.url("admin/member/memberList")
                            },
                            btn2: function () {
                                location.href = ns.url("admin/member/addMember")
                            }
                        });
                    }else{
                        layer.msg(res.message);
                    }
                }
            });
        });

        // 普通图片上传
        var headImg_upload = new Upload({
            elem: '#headImg'
        });

    });

    function back() {
        location.href = ns.url("admin/member/memberList");
    }
</script>
{/block}